//
//  ViewController.m
//  MyReader
//
//  Created by hh on 15/12/7.
//  Copyright © 2015年 hh. All rights reserved.
//

#import "ViewController.h"
#import "BookTableViewCell.h"
#import "ReadViewController.h"

@interface ViewController ()
{
    NSArray<NSURL *> *imgs;
    NSArray<NSURL *> *texts;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"我的书架";
    self.tableView.showsVerticalScrollIndicator = NO;
    NSBundle *bundle = [NSBundle mainBundle];
    imgs = [bundle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    texts = [bundle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!imgs || imgs.count == 0) {
        return 0;
    }
    NSUInteger count = imgs.count - 1;
    return count/3 + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    NSInteger index = row * 3;
    
    for (int i = 0; i < 3 && index + i < imgs.count; i++) {
        UIImage *img = [UIImage imageNamed:imgs[index + i].lastPathComponent];
        UIImageView *iv = [[UIImageView alloc] initWithImage:img];
        CGFloat edge = 15;
        CGFloat width = (self.view.frame.size.width - edge*4)/3;
        iv.frame = CGRectMake(edge + (width + edge)*i, 8, width, 139 - 8 - 8*2);
        [cell addSubview:iv];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = iv.bounds;
        btn.tag = 100 + index + i;
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:btn];
        iv.userInteractionEnabled = YES;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}

- (void)onClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 100;
    NSURL *imgURL = imgs[index];
    NSString *fileName = imgURL.lastPathComponent;
    fileName = fileName.stringByDeletingPathExtension;
    NSURL *tURL;
    for (NSURL *url in texts) {
        if ([url.absoluteString rangeOfString:fileName].location != NSNotFound) {
            tURL = url;
            break;
        }
    }
    
    NSError *err;
    NSString *content = [[NSString alloc] initWithContentsOfURL:tURL encoding:NSUTF8StringEncoding error:&err];
    if (err) {
        NSLog(@"%@",err.description);
    }
    ReadViewController *con = [ReadViewController new];
    con.text = content;
    con.hidesBottomBarWhenPushed = NO;
    [self.navigationController pushViewController:con animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
